/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.ai;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import rpg.game.SingleGame;
import rpg.Location;
import rpg.entities.Critter;
import rpg.entities.Entity;
import rpg.entities.EntityHandler;
import rpg.entities.LivingEntity;
import rpg.entities.Player;
import rpg.entities.area.Renderable;
import rpg.util.AStar;

/**
 *
 * @author rothens
 */
public class AIFollowIfAttacked extends AI {

    private AStar as;
    private LinkedList<Location> path;
    int nextCheck = 3;
    private int damageDelay = 0;

    public AIFollowIfAttacked(LivingEntity entity, int speed) {
        super(entity);
        as = new AStar();
        this.speed = speed;
    }

    @Override
    public boolean tick() {
        if (entity == null || entity.getHealth() <= 0) {
            return false;
        }

        if (damageDelay > 0) {
            damageDelay--;
        }

        if (entity.getTargetEntity() != null) {
            double dist = entity.getCurrentLocation().getDistance(entity.getTargetEntity().getCurrentLocation());
            if (dist <= 53) {
                attack();
                return true;
            } else if (dist > 320) {
                entity.setTargetEntity(null);
                if (path != null) {
                    path.clear();
                }
                return true;
            }
            if (path == null || path.isEmpty() || nextCheck <= 0) {
                path = as.getPath(entity.getCurrentLocation(), entity.getTargetEntity().getCurrentLocation(), entity.getMap());
                nextCheck = 3;
            }
            if (path == null || entity.getCurrentLocation() == null) {
                return true;
            }
            nextLoc = path.getFirst().normalize(entity.getCurrentLocation());
            if (entity.getCurrentLocation().equals(path.getFirst(), speed)) {
                path.removeFirst();
                nextCheck--;
            }
            speed = 2;
        } else {
            speed = 1;
            /*Renderable r = new Renderable(
                    Game.getInstance().getActual(),
                    Player.getInstance().getCurrentLocation().getX() + Player.getInstance().getxRenderOffset(),
                    Player.getInstance().getCurrentLocation().getY() + Player.getInstance().getyRenderOffset());*/
            for (Iterator<Entity> it = entity.getMap().entities.iterator(); it.hasNext();) {
                Entity e;
                try {
                    e = it.next();
                } catch (Exception ex) {
                    //if deleted when accessed
                    break;
                }
                if (e instanceof Critter
                        && e.getCurrentLocation().getDistance(entity.getCurrentLocation()) < 300
                       /* && e.isInView(r) && entity.isInView(r)*/) {
                    entity.setTargetEntity(e);
                    break;
                }
            }
        }
        animDelay--;
        if (animDelay <= 0) {
            animDelay = 5;
            animState++;
            animState %= 3;
        }
        int moveDir = entity.getCurrentLocation().getDir(nextLoc);
        if (moveDir != -1) {

            dir = moveDir;
            if (!entity.getCurrentLocation().move(moveDir, speed)) {
                if (entity.getTargetEntity() != null) {
                    path = as.getPath(entity.getCurrentLocation(), nextLoc, entity.getMap());
                } else {
                    nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
                }
            }
        } else {
            Random rand = new Random();
            if (rand.nextBoolean()) {
                nextLoc = entity.getCurrentLocation().getPossibleLocation(entity.getMap());
            }
        }

        if (damageDelay > 0) {
            damageDelay--;
        }
        
        return true;
    }

    public void attack() {
        if (damageDelay != 0) {
            return;
        }
        Entity target = entity.getTargetEntity();
        if (target != null && target instanceof LivingEntity) {
            damageDelay = 30;
            Random r = new Random();
            int dmg = r.nextInt(10) + 10;
            boolean damage = ((LivingEntity) target).damage(entity, dmg);
            if (damage) {
                if (target != null) {
                    EntityHandler.getInstance().removeEntity(target);
                }
                entity.setTargetEntity(null);
                if (path != null) {
                    path.clear();
                }
            }
        }
    }
}
